home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / Chess / Fly_Chess.jar / CommandsForm.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-08-23  |  5.9 KB  |  251 lines

  1. import javax.microedition.lcdui.Command;
  2. import javax.microedition.lcdui.CommandListener;
  3. import javax.microedition.lcdui.Display;
  4. import javax.microedition.lcdui.Displayable;
  5. import javax.microedition.lcdui.Image;
  6. import javax.microedition.lcdui.List;
  7.  
  8. public class CommandsForm implements CommandListener, ChessDisplayable, ServletResponseReceiver {
  9.    private boolean responsereceived = false;
  10.    private String response = null;
  11.    private ChessLogic chesslogic = null;
  12.    private ChessDisplayable chessreturn = null;
  13.    private List myScreen;
  14.    private Command OKCommand;
  15.    private Command backCommand;
  16.  
  17.    public CommandsForm(ChessLogic var1, ChessDisplayable var2) {
  18.       this.chesslogic = var1;
  19.       this.chessreturn = var2;
  20.       this.initialize();
  21.    }
  22.  
  23.    private void alert(String var1) {
  24.       MessageForm var2 = new MessageForm("Alert", var1, this.chessreturn);
  25.       var2.makeCurrent(CU.getDisplay());
  26.    }
  27.  
  28.    private boolean checkPostActive() {
  29.       ChessGame var1 = this.chesslogic.getChessGame();
  30.       if (var1 == null) {
  31.          return false;
  32.       } else if (var1.postActive()) {
  33.          this.alert("Unable to send command, still postthread active. \nStatus: " + var1.getStatus());
  34.          return true;
  35.       } else {
  36.          return false;
  37.       }
  38.    }
  39.  
  40.    public void commandAction(Command var1, Displayable var2) {
  41.       if (var1 != this.OKCommand && var1 != List.SELECT_COMMAND) {
  42.          if (var1 == this.backCommand) {
  43.             if (this.chesslogic.getChessGame() != null) {
  44.                this.chesslogic.getChessGame().setServletRedirection(this.chesslogic);
  45.             }
  46.  
  47.             this.chessreturn.makeCurrent(CU.getDisplay());
  48.          }
  49.  
  50.       } else {
  51.          this.executeCommand(this.myScreen.getString(this.myScreen.getSelectedIndex()));
  52.       }
  53.    }
  54.  
  55.    private void executeCommand(String var1) {
  56.       ChessGame var2 = this.chesslogic.getChessGame();
  57.       if (var2 != null) {
  58.          var2.setServletRedirection(this);
  59.       }
  60.  
  61.       if (var1.equals("Logon")) {
  62.          if (this.checkPostActive()) {
  63.             return;
  64.          }
  65.  
  66.          LogonForm var3 = new LogonForm(this.chesslogic, this);
  67.          var3.makeCurrent(CU.getDisplay());
  68.       } else if (var1.equals("Match player")) {
  69.          if (this.checkPostActive()) {
  70.             return;
  71.          }
  72.  
  73.          var2.doPost("who a", false);
  74.       } else if (var1.equals("Resign from game")) {
  75.          if (this.checkPostActive()) {
  76.             return;
  77.          }
  78.  
  79.          var2.doPost("resign", true);
  80.       } else if (var1.equals("Offer draw")) {
  81.          if (this.checkPostActive()) {
  82.             return;
  83.          }
  84.  
  85.          var2.doPost("draw", true);
  86.       } else if (var1.equals("Abort from game")) {
  87.          if (this.checkPostActive()) {
  88.             return;
  89.          }
  90.  
  91.          var2.doPost("abort", true);
  92.       } else if (var1.equals("Refresh board")) {
  93.          if (this.checkPostActive()) {
  94.             return;
  95.          }
  96.  
  97.          var2.doPost("refresh", false);
  98.       } else if (var1.equals("Nextstate")) {
  99.          if (this.checkPostActive()) {
  100.             return;
  101.          }
  102.  
  103.          var2.doPost("nextstate", false);
  104.       } else if (var1.equals("Accept draw-offer")) {
  105.          if (this.checkPostActive()) {
  106.             return;
  107.          }
  108.  
  109.          var2.doPost("accept", true);
  110.       } else if (var1.equals("Decline draw-offer")) {
  111.          if (this.checkPostActive()) {
  112.             return;
  113.          }
  114.  
  115.          var2.doPost("decline", true);
  116.       } else if (var1.equals("Review match-offer")) {
  117.          MessageForm var4 = new MessageForm("Match offered", var2.viewMatchIssue(), this);
  118.          var4.makeCurrent(CU.getDisplay());
  119.       } else if (var1.equals("Accept challenge")) {
  120.          if (this.checkPostActive()) {
  121.             return;
  122.          }
  123.  
  124.          var2.doPost("accept", true);
  125.       } else if (var1.equals("Decline challenge")) {
  126.          if (this.checkPostActive()) {
  127.             return;
  128.          }
  129.  
  130.          var2.doPost("decline", true);
  131.       } else if (var1.equals("Review challenge")) {
  132.          MessageForm var5 = new MessageForm("Challenged by", var2.viewChallenge(), this);
  133.          var5.makeCurrent(CU.getDisplay());
  134.       } else if (var1.equals("Poll")) {
  135.          if (this.checkPostActive()) {
  136.             return;
  137.          }
  138.  
  139.          var2.doPost("poll", false);
  140.       } else if (var1.equals("Logoff")) {
  141.          if (this.checkPostActive()) {
  142.             return;
  143.          }
  144.  
  145.          var2.doPost("logoff", true);
  146.          this.chesslogic.reset();
  147.          this.chesslogic.setChessGame((ChessGame)null);
  148.       } else if (var1.equals("Exit")) {
  149.          CU.exitMIDlet();
  150.       } else if (var1.equals("About")) {
  151.          CU.displayChessMIDlet();
  152.       } else if (var1.equals("Setup Servlet URL")) {
  153.          SetupServlet var6 = new SetupServlet(this.chesslogic, this);
  154.          var6.makeCurrent(CU.getDisplay());
  155.       } else if (var1.equals("Setup Chess Engine")) {
  156.          SetupChess var7 = new SetupChess(this.chesslogic, this);
  157.          var7.makeCurrent(CU.getDisplay());
  158.       } else if (var1.equals("Post History")) {
  159.          MessageForm var8 = new MessageForm("Post History", PostThread.getPostHistory(), this);
  160.          var8.makeCurrent(CU.getDisplay());
  161.       }
  162.  
  163.       if (var2 != null) {
  164.          this.myScreen.setTitle(var2.getStatus());
  165.       }
  166.  
  167.    }
  168.  
  169.    public void initialize() {
  170.       this.myScreen = new List("Choose Command", 3);
  171.       this.OKCommand = new Command("OK", 4, 1);
  172.       this.backCommand = new Command("Chess", 2, 1);
  173.       if (this.chesslogic.getChessGame() == null) {
  174.          this.myScreen.append("Logon", (Image)null);
  175.       } else {
  176.          ChessGame var1 = this.chesslogic.getChessGame();
  177.          if (!var1.isPlaying()) {
  178.             this.myScreen.append("Match player", (Image)null);
  179.          } else {
  180.             this.myScreen.append("Resign from game", (Image)null);
  181.             this.myScreen.append("Offer draw", (Image)null);
  182.             this.myScreen.append("Abort from game", (Image)null);
  183.             this.myScreen.append("Refresh board", (Image)null);
  184.             this.myScreen.append("Nextstate", (Image)null);
  185.             if (var1.drawOffered()) {
  186.                this.myScreen.append("Accept draw-offer", (Image)null);
  187.                this.myScreen.append("Decline draw-offer", (Image)null);
  188.             }
  189.          }
  190.  
  191.          if (var1.matchIssued()) {
  192.             this.myScreen.append("Review match-offer", (Image)null);
  193.          }
  194.  
  195.          if (var1.isChallenged()) {
  196.             this.myScreen.append("Accept challenge", (Image)null);
  197.             this.myScreen.append("Decline challenge", (Image)null);
  198.             this.myScreen.append("Review challenge", (Image)null);
  199.          }
  200.  
  201.          this.myScreen.append("Poll", (Image)null);
  202.          this.myScreen.append("Logoff", (Image)null);
  203.       }
  204.  
  205.       this.myScreen.append("Exit", (Image)null);
  206.       this.myScreen.append("About", (Image)null);
  207.       this.myScreen.append("Setup Servlet URL", (Image)null);
  208.       this.myScreen.append("Setup Chess Engine", (Image)null);
  209.       this.myScreen.append("Post History", (Image)null);
  210.       this.myScreen.addCommand(this.OKCommand);
  211.       this.myScreen.addCommand(this.backCommand);
  212.       ChessGame var2 = this.chesslogic.getChessGame();
  213.       if (var2 != null && var2.postActive()) {
  214.          this.myScreen.setTitle(var2.getStatus());
  215.       }
  216.  
  217.    }
  218.  
  219.    public boolean isActive() {
  220.       return CU.getDisplay().getCurrent() == this.myScreen;
  221.    }
  222.  
  223.    public void makeCurrent(Display var1) {
  224.       this.initialize();
  225.       var1.setCurrent(this.myScreen);
  226.       this.myScreen.setCommandListener(this);
  227.    }
  228.  
  229.    public void receiveServletResponse(String var1) {
  230.       this.response = var1;
  231.       if (CU.firstToken(var1).equals("who")) {
  232.          MatchForm var2 = new MatchForm(CU.removeFirstTokens(var1, 1), this.chesslogic, this);
  233.          var2.makeCurrent(CU.getDisplay());
  234.       } else if (CU.firstToken(var1).equals("nextstate")) {
  235.          if (this.chesslogic.getChessGame() != null) {
  236.             this.chesslogic.getChessGame().setServletRedirection(this.chesslogic);
  237.          }
  238.  
  239.          this.chessreturn.makeCurrent(CU.getDisplay());
  240.       } else if (CU.firstToken(var1).equals("refresh")) {
  241.          if (this.chesslogic.getChessGame() != null) {
  242.             this.chesslogic.getChessGame().setServletRedirection(this.chesslogic);
  243.          }
  244.  
  245.          this.chessreturn.makeCurrent(CU.getDisplay());
  246.       }
  247.  
  248.       this.responsereceived = true;
  249.    }
  250. }
  251.